﻿#include "VideoRenderViewWindow.h"
#include <QFileDialog>
#include <QSize>
#include "VideoRenderViewController.h"
//#include "../../ffmpeg/utils/FFmpegUtils.h"

VideoRenderViewWindow::VideoRenderViewWindow(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	setAttribute(Qt::WA_DeleteOnClose, true);  // 关闭时自动销毁窗口
	this->setWindowTitle("SDL播放视频文件综合案例");
	//FFmpegUtils::Get()->GetFrame(0,0,nullptr)->width
}



void VideoRenderViewWindow::openFileAndStart()
{
	isResizeFlag = true;
	QString filter = "RGB 文件 (*.*);;所有文件 (*.*)";
	// 打开文件选择对话框，选择单个文件
	QString filePath = QFileDialog::getOpenFileName(this, "选择 RGB 文件", "", filter);
	if (filePath.isEmpty()) {
		return;
	}
	mWidth = ui.lineEditWidth->text().toInt();
	mHeight = ui.lineEditHeight->text().toInt();
	this->resize(QSize(mWidth, mHeight));
	ui.labelVideo->resize(mWidth, mHeight);
	VideoRenderViewController::Get()->Close();
	VideoRenderViewController::Get()->mWidth = mWidth;
	VideoRenderViewController::Get()->mHeight = mHeight;
	VideoRenderViewController::Get()->fps = ui.lineEditFps->text().toInt();
	VideoRenderViewController::Get()->mPixFormat = m_fmt;
	VideoRenderViewController::Get()->win_id = (void*)ui.labelVideo->winId();
	VideoRenderViewController::Get()->mFilePath = filePath;
	VideoRenderViewController::Get()->Init();
	VideoRenderViewController::Get()->Start();
	//VideoRenderViewController::Get()->Draw();
}

void VideoRenderViewWindow::onSelectPixFormatIndex(int pos)
{
	switch (pos)
	{
	case 0:
		m_fmt = IVideoRenderView::ARGB;
		break;
	case 1:
		m_fmt = IVideoRenderView::RGBA;
		break;
	case 2:
		m_fmt = IVideoRenderView::RGB24;
		break;
	case 3:
		m_fmt = IVideoRenderView::YUV420P;
		break;
	}

}

void VideoRenderViewWindow::closeEvent(QCloseEvent* event) {
	event->accept();  // 接受关闭事件（允许关闭）
	VideoRenderViewController::Get()->Close();
	qDebug() << "窗口已关闭";
}

void VideoRenderViewWindow::resizeEvent(QResizeEvent* event) {
	// 调用父类实现（保留默认行为）
	QWidget::resizeEvent(event);
	if (isResizeFlag) {
		// 获取窗口新尺寸（event->size() 是QSize类型）
		if (mWidth < 1920 || mHeight < 1080) {
			mWidth = event->size().width();
			mHeight = event->size().height();
			ui.labelVideo->resize(mWidth, mHeight);
			ui.labelVideo->move(0, 0);
		}
		
		//VideoRenderViewController::Get()->Resize(mWidth, mWidth*0.75);
		qDebug() << "窗口大小改变了,width=" << mWidth << "，height=" << this->mHeight;
	}

}

VideoRenderViewWindow::~VideoRenderViewWindow()
{
	qDebug() << "VideoRenderViewWindow析构函数已执行";
}
